package com.argrace.smart.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.WangGuanDeviceEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_DeviceNameAndRoomNameTable;
import com.argrace.smart.ui.DrawableSwitch;
import com.argrace.smart.util.AlinkHttpUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/3/2.
 */

public class MyRoomDeviceAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private LayoutInflater inflater;
    private Context mContext;
    private List<LocalDb_DeviceNameAndRoomNameTable> mDatas;
    ArrayList<String> lists_uuid = new ArrayList<>();

    //创建构造参数
    public MyRoomDeviceAdapter(Context context, List<LocalDb_DeviceNameAndRoomNameTable> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(context);
    }

    //创建ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_myroomdevice, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    //绑定ViewHolder
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        //为textview 赋值
        holder.device_name.setText(mDatas.get(position).getNickName());
        holder.device_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int state = holder.device_switch.isSwitchOn() ? 0 : 1;
                holder.device_switch.setSwitchOn(!holder.device_switch.isSwitchOn());
                holder.device_switch.invalidate();
                sendMsg(mDatas.get(position).getProperty(), String.valueOf(state), mDatas.get(position));
            }
        });
        if (mDatas.get(position).getSwitch_state().equals("0")) {
            holder.device_switch.setSwitchOn(false);
        } else {
            holder.device_switch.setSwitchOn(true);
        }
        if (mDatas.get(position).isExpand()) {
            holder.device_switch.setVisibility(View.VISIBLE);
        } else {
            holder.device_switch.setVisibility(View.GONE);
        }

        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.click(mDatas.get(position), position);
                }
            }
        });

        holder.image_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.device_switch.getVisibility() == View.GONE) {
                    getDeviceDetailInfo(mDatas.get(position).getUuid(), new ALinkBusinessEx.IListener() {
                        @Override
                        public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                            if (transitoryResponse == null || transitoryResponse.data == null) {
                                return;
                            }
                            holder.DeviceInfo = transitoryResponse.data.toString();
                            JSONObject obj_data = JSONObject.parseObject(holder.DeviceInfo);
                            LocalDb_DeviceNameAndRoomNameTable entity = mDatas.get(position);
                            if (obj_data != null && obj_data.getString("uuid").equals(entity.getUuid())) {
                                switch (entity.getModel()) {
                                    case ConstantConfigure.MODEL_10ACHAZUO:
                                        if (entity.getProperty().equals("SwitchMultiple_1")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("SwitchMultiple_1"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("SwitchMultiple_2")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("SwitchMultiple_2"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("SwitchChildLock")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("SwitchChildLock"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        break;
                                    case ConstantConfigure.MODEL_SANWEIDUOKONG:
                                        if (entity.getProperty().equals("Switch_1")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("Switch_1"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("Switch_2")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("Switch_2"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("Switch_3")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("Switch_3"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        break;
                                    case ConstantConfigure.MODEL_LIANGWEIDUOKONG:
                                        if (entity.getProperty().equals("Switch_1")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("Switch_1"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("Switch_2")) {
                                            Key_ValueEntity chazuo_switch1 = JSON.parseObject(obj_data.getString("Switch_2"), Key_ValueEntity.class);
                                            entity.setSwitch_state(chazuo_switch1.getValue());
                                        }
                                        break;
                                    case ConstantConfigure.MODEL_MENCIGANYING:
                                        Key_ValueEntity menci_switch = JSON.parseObject(obj_data.getString("ContactAlarm"), Key_ValueEntity.class);
                                        if (menci_switch != null) {
                                            entity.setSwitch_state(menci_switch.getValue());
                                        }
                                        break;
                                    case ConstantConfigure.MODEL_KONGTIAOZHUANHUANQI:
                                        if (entity.getProperty().equals("1")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_1"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("2")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_2"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("3")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_3"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("4")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_4"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("5")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_5"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        if (entity.getProperty().equals("6")) {
                                            Key_ValueEntity kongtiao_switch1 = JSON.parseObject(obj_data.getString("Switch_6"), Key_ValueEntity.class);
                                            entity.setSwitch_state(kongtiao_switch1.getValue());
                                        }
                                        break;
                                    default:
                                        Key_ValueEntity default_switch = JSON.parseObject(obj_data.getString("Switch"), Key_ValueEntity.class);
                                        if (default_switch != null) {
                                            entity.setSwitch_state(default_switch.getValue());
                                        }
                                        break;
                                }
                            }
                        }

                        @Override
                        public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                            holder.DeviceInfo = null;
                        }
                    });
                    holder.device_switch.setVisibility(View.VISIBLE);
                    holder.image_arrow.setImageResource(R.mipmap.arrow_up);
                } else {
                    holder.device_switch.setVisibility(View.GONE);
                    holder.image_arrow.setImageResource(R.mipmap.arrow_down);
                }
            }
        });
    }

    private void sendMsg(String key, String value, LocalDb_DeviceNameAndRoomNameTable entity) {
        Log.i("info", "RoomDevice发送的数据是key = " + key + "----" + "value=" + value + "****uuid=" + entity.getUuid());
        String method = "mtop.openalink.app.core.device.set.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", entity.getUuid());
        params.put("setParams", "{\"" + key + "\":\"" + value + "\"}");
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                Log.i("info", "发送消息成功" + transitoryResponse.data);
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "发送消息失败");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();

    }

    //新增item
    public void addData(int pos) {
//        mDatas.add("新增");
        notifyItemInserted(pos);
    }

    //移除item
    public void deleateData(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }

    //查询设备最新状态
    private void getDeviceDetailInfo(String uuid, ALinkBusinessEx.IListener listener) {
        String method = "mtop.openalink.app.core.device.get.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", uuid);
        AlinkHttpUtil.AlinkHttpRequest(method, params, listener);
    }

    private OnAdapterItemClickListener listener;

    public interface OnAdapterItemClickListener {
        void click(LocalDb_DeviceNameAndRoomNameTable bean, int position);
    }

    public void setListeners(OnAdapterItemClickListener l) {
        this.listener = l;
    }

    public ArrayList<String> getLists_uuid() {
        lists_uuid.clear();
        for (int i = 0; i < mDatas.size(); i++) {
            lists_uuid.add(mDatas.get(i).getUuid());
        }
        return lists_uuid;
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView device_name;
    DrawableSwitch device_switch;
    String DeviceInfo = null;
    ImageView image_arrow;
    RelativeLayout rl;

    public MyViewHolder(View itemView) {
        super(itemView);
        device_name = (TextView) itemView.findViewById(R.id.item_myroomdevice_devicename);
        device_switch = (DrawableSwitch) itemView.findViewById(R.id.item_myroomdevice_switch);
        image_arrow = (ImageView) itemView.findViewById(R.id.item_myroomdevice_arrow);
        rl = (RelativeLayout) itemView.findViewById(R.id.item_myroomdevice_rl);
    }
}